package com.ccdt.tv.smartrecommend.utils;

import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.util.Log;

import com.blankj.utilcode.util.FileIOUtils;
import com.blankj.utilcode.util.SPUtils;
import com.ccdt.tv.smartrecommend.Constant;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * Created by StormSun on 15,七月,2016
 * <p>
 * 打开直播时传参用
 * 只在开机第一次打开直播时调用
 */
public class DvbVodInfoManager {

    private static final String TAG = "DvbVodInfoManager";

    private static final String DVB_VOD_INFO_PATH = "/resource/misc/dvb_playinfo.txt";

    //开机15秒解析跳转直播文件地址
    private static final String DVB_VOD_BOOT_PATH = "/data/launcherChannel.json";

    private static final String KEY = "dvb_param";
    private static  String DVB_SAVE_DATA="";
    public static void initDvbParams(Context context) {
        try {
            String fileContent = FileIOUtils.readFile2String(DVB_VOD_INFO_PATH, "UTF-8");
            if (fileContent == null) {
                Log.i(TAG, "initDvbParams:file is null!");
                return;
            }
            Log.i(TAG, "initDvbParams:" + fileContent);
            String replace = "";
            if (!TextUtils.isEmpty(fileContent) && fileContent.trim().length() > 0) {
                replace = fileContent.replaceAll("[\\t\\n\\r]", "@");//将内容区域的回车换行去除

            }
            Log.i(TAG, "initDvbParams: dvb_param put to sp -> " + replace);
            SPUtils.getInstance(Constant.SP_NAME).put(KEY, replace);
        } catch (Exception e) {
            Log.i(TAG, "initDvbParams:error!");
            return;
        }
    }



    //解析文件
//    public static BootBean getDvbBootUrl(Context context){
//        try{
//            String fileContent = FileUtils.readFile2String(DVB_VOD_BOOT_PATH, "UTF-8");
//            if (fileContent == null) {
//                LogHelper.i(TAG, "initDvbParams:file is null!");
//                return null;
//            }
//            Gson gson = new Gson();
//            BootBean result = gson.fromJson(fileContent, BootBean.class);
//            return result;
//        }catch (Exception e){
//            return null;
//        }
//
//    }

    public static String getDvbUrl(Context context) {
        // 重新加载一次配置文件
        initDvbParams(context);
        String ret = "";
        String replace = SPUtils.getInstance(Constant.SP_NAME).getString(KEY, "");
        Log.i(TAG, "DVB_VOD_INFO_PATH dvb_param get from sp -> " + replace);
        String[] split = null;
        if (!TextUtils.isEmpty(replace) && replace.trim().length() > 0) {
            split = replace.split("@@");
        }



        if (split != null && split.length > 0) {
            SimpleDateFormat sdf = new SimpleDateFormat("yyMMdd");
            String format = sdf.format(new Date());
            for (String s : split) {
                Log.i(TAG, "DVB_VOD_INFO_PATH >>> " + s);
                if (!TextUtils.isEmpty(s) && s.trim().length() > 0) {
                    String[] split1 = s.split("=");
                    if (split1.length == 2) {
                        if (format.equals(split1[0])) {
                            ret = split1[1];
                            break;
                        }
                    }
                }
            }
        }

        if (TextUtils.isEmpty(ret) || ret.trim().length() <= 0) {
            ret = null;
        }
        Log.i(TAG, "DVB_VOD_INFO_PATH getDvbUrl -> " + ret);
        return ret;
    }


//    public static BootBean getBootbean(Context context){
//        BootBean bootBean = DvbVodInfoManager.getDvbBootUrl(context);
//        return bootBean;
//    }
    //开机15秒跳转直播
//    public static void bootOpenPlayer(Context context, BootBean bootBean){
//        SimpleDateFormat sdf = new SimpleDateFormat("yyMMdd");
//        String nowDate = sdf.format(new Date());
//        if (bootBean!=null){
//            for (int i =0;i<bootBean.getGotodvb().size();i++) {
//                if (bootBean.getGotodvb().get(i).getTime().equals(nowDate)){
//                    Intent intent = new Intent();
//                    int networkid = Integer.parseInt(bootBean.getGotodvb().get(i).getNetworkid());
//                    int tsid = Integer.parseInt(bootBean.getGotodvb().get(i).getTsid());
//                    int serviceid = Integer.parseInt(bootBean.getGotodvb().get(i).getServiceid());
//                    intent.putExtra("networkid", networkid);
//                    intent.putExtra("tsid", tsid);
//                    intent.putExtra("serviceid", serviceid);
//                    openDvbPlayer(context, intent);
//                }
//            }
//        }
//    }


    /**
     * 非小视频窗口进入直播
     *
     * @param context

     */
    public static boolean openDvbPlayer(Context context) {
        try {
            String url = null;
            //String date = (String) SPUtil.get(context, "LauncharName_date", "");
            String date = DVB_SAVE_DATA;
            Log.i(TAG, "openDvbPlayer: dvb button open dvb player apk, get LauncharName_date from sp -> " + date);
            SimpleDateFormat sdf = new SimpleDateFormat("yyMMdd");
            String nowDate = sdf.format(new Date());

                if (!nowDate.equals(date)) {
                    url = DvbVodInfoManager.getDvbUrl(context);
                    Log.i(TAG, "openDvbPlayer: dvb button open dvb player apk, put LauncharName_date to sp -> " + nowDate);
                    DVB_SAVE_DATA= nowDate;
                }
            Intent intent = new Intent();
            if (url != null && url.trim().length() > 0) {
                String[] arr = url.split(":");
                if (arr != null && arr.length == 3) {
                    int networkid = Integer.parseInt(arr[0]);
                    int tsid = Integer.parseInt(arr[1]);
                    int serviceid = Integer.parseInt(arr[2]);
                    intent.putExtra("networkid", networkid);
                    intent.putExtra("tsid", tsid);
                    intent.putExtra("serviceid", serviceid);
                    openDvbPlayer(context, intent);
                    Log.i(TAG, "openDvbPlayer: dvb button open dvb player apk,  networkid=" + networkid + ", tsid=" + tsid + ", serviceid=" + serviceid);
                    return true;
                }
             }
            Log.i(TAG, "openDvbPlayer: dvb button open dvb player apk ,url -> " + url);
        } catch (Exception e) {
            Log.i(TAG, "openDvbPlayer: error!");
            e.printStackTrace();
        }
        return false;
    }

    /**
     * 打开DVB直播[外部apk]
     *
     * @param context
     */
    public static void openDvbPlayer(Context context, Intent intent) {
        String pkgName = "cn.ccdt.dvbplayer";
        String clsName = "cn.ccdt.dvbplayer.PlayActivity";
        Log.i(TAG, "通过[包名+类名]打开apk：\n    pkgName=" + pkgName + "\n   clsName=" + clsName);

        ComponentName componentName = new ComponentName(pkgName, clsName);
        intent.setComponent(componentName);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        context.startActivity(intent);


//        String action = "cn.ccdt.DvbTv";
//        LogUtil.d("通过[Action]打开apk  action = " + action);
//        intent.setAction(action);
//        context.startActivity(intent);


    }

}
